﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Mighty.Common;
using System.Threading;

namespace Mighty.Brain
{
    public class GoriBrain : BasicBrain
    {
        /// <summary>
        /// 자신의 베팅차례에서 제일 많은 무늬를 기루다로 선택한다
        /// </summary>
        public override Betting BettingTicEvent()
        {
            Betting betting = new Betting();
            betting.isPass = true;

            // 각 무늬의 개수를 센다
            int heart = 0;
            int clover = 0;
            int spade = 0;
            int diamond = 0;

            int grim_heart = 0;
            int grim_clover = 0;
            int grim_spade = 0;
            int grim_diamond = 0;

            int grim_count = 0;
    
            for (int i = 0; i < 10; i++)
            {
                HandedCard card = gameInfo.myCards[i];
                if (card.shape == Shape.Clover){
                    clover++;
                    if (card.number >= 10) grim_clover++;
                }
                else if (card.shape == Shape.Heart){
                    heart++;
                    if (card.number >= 10) grim_heart++;
                }
                else if (card.shape == Shape.Spade){
                    spade++;
                    if (card.number >= 10) grim_spade++;
                }
                else if (card.shape == Shape.Diamond){
                    diamond++;
                    if (card.number >= 10) grim_diamond++;
                }
            }


            // 가장 많은 무늬를 찾아 낸다
            Giruda selectedShape = Giruda.Heart;
            int selectedCount = heart;
            grim_count = grim_heart;

            if (clover > selectedCount)
            {
                selectedShape = Giruda.Clover;
                selectedCount = clover;
                grim_count = grim_clover;
            }
            if (spade > selectedCount)
            {
                selectedShape = Giruda.Spade;
                selectedCount = spade;
                grim_count = grim_spade;
            }
            if (diamond > selectedCount)
            {
                selectedShape = Giruda.Diamond;
                selectedCount = diamond;
                grim_count = grim_diamond;

            }

            betting.giruda = selectedShape;


            if (selectedCount >= 5 && grim_count >= 3)
            {


                // 15이하로 베팅한다
                if (gameInfo.betting.limit > 0)
                {
                    Betting previousBetting = (Betting)gameInfo.bettingHistory[gameInfo.bettingHistory.Count - 1];
                    if (previousBetting.limit < 15)
                    {
                        betting.isPass = false;
                        betting.limit = previousBetting.limit + 1;
                    }
                }
                else
                {
                    betting.isPass = false;
                    betting.limit = 13;
                }
            }
            else betting.isPass = true;
 
                return betting;
        }

        public override FriendPlayer ChoiceFriendEvent()
        {
            for (int i = 0; i <= 9; i++)
            {
                if(gameInfo.myCards[i] != gameInfo.mighty)
                {
                    gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                    gameInfo.friend.cardCondition = gameInfo.mighty;        
                }
                else if (gameInfo.myCards[i].shape != Shape.Joker)
                {
                    Card card = new Card();
                    card.shape = Shape.Joker;
                    gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                    gameInfo.friend.cardCondition = card;
                }
                else if (gameInfo.myCards[i] != gameInfo.jokerCall)
                {
                    gameInfo.friend.howToSelect = FriendPlayer.HowToSelect.card;
                    gameInfo.friend.cardCondition = gameInfo.jokerCall;
                }
            }   
            return gameInfo.friend;
        }
        public override HandedCard RoundTicEvent()
        {
            List<HandedCard> possibleCards = new List<HandedCard>();
            foreach (HandedCard card in gameInfo.myCards)
            {
                if (card.isPossible) possibleCards.Add(card);
            }
            
            int k = 0;
            int max = 0;




            for (int i = 0; i < possibleCards.Count ; i++)
            {
                if (gameInfo.myCards[i] != gameInfo.mighty && gameInfo.myCards[i].shape != Shape.Joker && max < possibleCards[i].number && possibleCards[i].shape == (Shape)gameInfo.betting.giruda)
                {                    
                    k = i;
                    max = possibleCards[i].number;
                }
                
            }
            return possibleCards[k];
        }
    }    
}
